<?php

namespace app\admin\controller\card;

use app\common\controller\Backend;

use think\Controller;
use think\Request;
use think\Db;
use app\mpbase\library\WechatCard;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Consumecard extends Backend
{
    
    /**
     * ConsumeCard模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('ConsumeCard');

    }

    //重写index方法
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        //展示数据月份的选择
        $month = input('month',date('Y-m'));
        $begin = $month.'-01';
        $start_time = strtotime($month);
        $end_time = strtotime("$begin +1 month");
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            //字段
            $field = ['consume.id','user.nickname','user.avatar','card.title','store.s_name','consume.createtime','send_store.s_name as send_store_name','consume.code','card.reduce_cost','cancel_user.nickname as cancel_nickname','cancel_user.avatar as cancel_avatar'];
            //条件
            $consume_where = ['card.admin_id' => session('admin.id')];

//            $total_model = Db::table('uct_consume_card')->alias('consume')
//                ->join('uct_wechat_card card','card.card_id = consume.card_id')
//                ->join('uct_hk_store store','store.id = consume.cancel_store_id')
//                ->join('uct_user user','user.mp_openid = consume.user_openid')
//                ->where($where);

            //应用id
            $mp_id = get_admin_applet_info()['id'];

            $total_model = Db::table('uct_consume_card')->alias('consume')
                ->join('uct_wechat_card card','card.card_id = consume.card_id and consume.createtime between '.$start_time.' and '.$end_time)
                ->join('uct_hk_store store','store.id = consume.cancel_store_id')
                ->join('uct_hk_store send_store','send_store.id = card.store_id')
                ->join('uct_user user','user.mp_openid = consume.user_openid and user.mp_id = '.$mp_id)
                ->join('uct_user cancel_user','cancel_user.mp_openid = consume.user_openid and cancel_user.mp_id <> '.$mp_id)
                ->where($where);

            $list_model = Db::table('uct_consume_card')->alias('consume')
                ->join('uct_wechat_card card','card.card_id = consume.card_id and consume.createtime between '.$start_time.' and '.$end_time)
                ->join('uct_hk_store store','store.id = consume.cancel_store_id')
                ->join('uct_hk_store send_store','send_store.id = card.store_id')
                ->join('uct_user user','user.mp_openid = consume.user_openid and user.mp_id = '.$mp_id)
                ->join('uct_user cancel_user','cancel_user.mp_openid = consume.user_openid and cancel_user.mp_id <> '.$mp_id)
                ->where($where)
                ->field($field)
                ->limit($offset, $limit);

            if(session('admin.type') == 0){//超级管理员
                $total = $total_model->count();
                $list = $list_model->select();
            }else{//广告商  商家
                $total = $total_model->where($consume_where)->count();
                $list = $list_model->where($consume_where)->select();
            }
            if(session('admin.type') != 0) {//非管理员
                foreach ($list as $key => &$val) {
                    $len = strlen($val['nickname']);
                    $first_str = mb_substr($val['nickname'], 0, 1, 'utf-8');
                    $val['nickname'] = $first_str . str_repeat('*', 4);
                }
            }
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        //时间列表固定死
        $date_list = get_date_list();
        $param = '&month='.$month;
        $this->view->assign('type',session('admin.type'));
        $this->view->assign('param',$param);
        $this->view->assign('month',$month);
        $this->view->assign('url',url());
        $this->view->assign('date_list',$date_list);
        $this->view->assign('start_time',$start_time);
        $this->view->assign('end_time',$end_time);
        return $this->view->fetch();
    }

    //测试获取用户信息
    public function user()
    {
        $card = new WechatCard();
        $user = $card->app->user;
        $userinfo = $user->get('o7EQD1m0JdIdwkfwcAWDS1qv6zBo');
        dump($userinfo);
    }
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
     * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

}
